package lib.pacificapp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.content.Context;
import android.os.Environment;

/**
 
 */
public class Filer {
	
	private static Filer manager;
	
	/**
	  */
	private static final long maxCacheTime = 1000*60*60*24*15;
	
	/**
	  * @param context
	 */
	public Filer(){
	}
	/**
	  * @param context
	 * @return
	 */
	public static Filer getIntance(){
		if(manager == null)
			manager = new Filer();
		return manager;
	}
	/**
	  * @param context	上下�?
	 * @return
	 */
	public String getCacheDir(Context context, String path){
		String dir = context.getCacheDir().getAbsolutePath()+path;
		File file = new File(dir);
		if(!file.exists())
			file.mkdirs();
		return dir;
	}
	
	/**
	  */
	public String getSDDir(String path){
		String url = null;
		if(!isSdCardAvailable())return null;
		url = Environment.getExternalStorageDirectory()+path;
		File file = new File(url);
		if(!file.exists())
			file.mkdirs();
		return url;
	}
	/**
	 	 */
	public String getSDOrCache(Context context, String path){
		String url = null;
		if(isSdCardAvailable())
			url = getSDDir(path);
		else
			url = getCacheDir(context, path);
		return url;
	}
	/**
 	 */
	public boolean isSdCardAvailable(){
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			return true;
		}
		return false;
	}
	/**
 	 * @return
	 */
	public String formatPath(String name){
		String path = name;
		path = path.replace(":", "_");
		path = path.replace("/", "_");
//		path = path.replace(".png", ".jms");
//		path = path.replace(".jpg", ".jms");
//		path = path.replace(".bmp", ".jms");
		return path;
	}
	/**
	 	 * @return
	 */
	public boolean isExists(String path){
		File file = new File(path);
		if(file.exists())
			return true;
		return false;
	}
	
	/**
	 	 */
	public void cleanInvalidCache(String basePath){
		File file = new File(basePath);
		if(!file.exists()) return;
		File[] caches = file.listFiles();
		if(caches == null || caches.length == 0) return;
		long now = System.currentTimeMillis();
		try {
			for(int i=0;i<caches.length;i++){
				if((now - caches[i].lastModified()) < maxCacheTime){
					continue;
				}
				caches[i].delete();
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				 	}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 	 */
	public void WriteObject(Object obj,Context context,String path) throws Exception {
		FileOutputStream fis = context.openFileOutput(path, Context.MODE_PRIVATE);
		ObjectOutputStream oos = new ObjectOutputStream(fis);
		oos.writeObject(obj);
		oos.close();
		fis.close();
	}

	/**
 	 * @return
	 * @throws Exception
	 */
	public Object readObject(Context context,String path) throws Exception {
		Object obj = new Object();
		FileInputStream fis = context.openFileInput(path);
		ObjectInputStream oos = new ObjectInputStream(fis);
		obj = oos.readObject();
		oos.close();
		fis.close();
		return obj;
	}
	
	/**
  	 */
	public void delete(String path){
		File file = new File(path);
		if(file.exists())
			file.delete();
	}
}
